package com.hd.crdao.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.google.gson.Gson;
import com.hd.crdao.R;
import com.hd.crdao.activity.LoanCarInfoActivity;
import com.hd.crdao.activity.NewLoanCarActivity;
import com.hd.crdao.adapter.LoanCarAdapter;
import com.hd.crdao.base.BaseFragment;
import com.hd.crdao.bean.LoanCarListBean;
import com.hd.crdao.config.Config;
import com.hd.crdao.config.Constants;
import com.hd.crdao.utils.ALog;
import com.hd.crdao.utils.MyToast;
import com.hd.crdao.utils.SPUtils;
import com.hd.crdao.utils.UIUtils;
import com.hd.crdao.view.RecyclerViewDivider;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

import okhttp3.Call;

/**
 *
 *@author Programming
 *@date 2017/7/10 0010
 */

public class LoanCarBaseFragment extends BaseFragment {
    RecyclerView mRecyclerView;
    public Handler mHandler = new Handler();
    public LoanCarAdapter mLoanCarAdapter;
    public List<LoanCarListBean.RowsBean> mLoanCarListBeanRows;
    public SmartRefreshLayout mRefreshLayout;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_loancar;
    }

    @Override
    protected void initView() {
        mRecyclerView = (RecyclerView) mViewRoot.findViewById(R.id.loanCarRecyclerview);
        mRefreshLayout = (SmartRefreshLayout) mViewRoot.findViewById(R.id.refreshLayout);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerView.addItemDecoration(new RecyclerViewDivider(UIUtils.getContext(),LinearLayoutManager.HORIZONTAL,1, Color.GRAY));
        mRefreshLayout.autoRefresh();
    }

    @Override
    protected void initListener() {
        mRefreshLayout.autoRefresh();
        mRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                initData();
                refreshlayout.finishLoadmore();
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                initData();
                refreshlayout.finishRefresh();
            }
        });
    }

    @Override
    protected void initData() {
        try {
            Bundle arguments = getArguments();
            final String state = arguments.getString("state");
            String businessid = SPUtils.getString(Config.businessId, "");
            // String businessid ="159bac3d50c64be2b822118706685cea";  //测试
            OkHttpUtils.post()
                    .addParams("data", businessid + "~" + state)
                    .addParams("sign", Constants.SIGN)
                    .url(Constants.LOANCAR_URL)
                    .build().execute(new StringCallback() {
                @Override
                public void onError(Call request, Exception e, int id) {
                    MyToast.show("网络开了个小差");
                }

                @Override
                public void onResponse(String response, int id) {

                    ALog.d(response);
                    LoanCarListBean loanCarListBean = new Gson().fromJson(response, LoanCarListBean.class);
                    if (loanCarListBean == null) {
                        return;
                    }
                    if (loanCarListBean.getSuccess() == 0) {
                        //设置适配器数据
                        mLoanCarListBeanRows = loanCarListBean.getRows();
                        mLoanCarAdapter = new LoanCarAdapter(mLoanCarListBeanRows);
                        mRecyclerView.setAdapter(mLoanCarAdapter);
                        ((NewLoanCarActivity)getActivity()).setNotice(loanCarListBean.getCreditTotal(),
                                loanCarListBean.getAuditTotal(),
                                loanCarListBean.getFinishTotal()
                        );
                                              mLoanCarAdapter.setOnItemClickListener(new LoanCarAdapter.OnItemClickListener() {
                            @Override
                            public void onItemClick(View v, int position) {
                                Intent intent = new Intent(UIUtils.getContext(), LoanCarInfoActivity.class);
                                LoanCarListBean.RowsBean rowsBean = mLoanCarListBeanRows.get(position);
                                intent.putExtra("carId", rowsBean.getId());
                                intent.putExtra("vehCreditId", rowsBean.getVehCredit().getId());
                                intent.putExtra("veBrand",rowsBean.getVeBrand());
                                intent.putExtra("veFrame",rowsBean.getVeFrame());
                                intent.putExtra("loanCarState",state);

                                startActivity(intent);
                            }
                        });
                    } else {
                        MyToast.show(loanCarListBean.getMessage());
                    }
                }
            });
        } catch (Exception e) {
            ALog.d(e.getMessage());
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()) {
            initData();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }
}
